package tsg.component;

import java.awt.event.ComponentEvent;
import java.awt.event.WindowEvent;

import tsg.graphic.PCGraphics;
import tsg.shape.Rectangle;
import tsg.util.Dimension;
import tsg.util.Point;


public class PCFrame implements SystemFrame
{
	protected PCFrame(String title, final Frame frame)
	{
		_frame = new javax.swing.JFrame(title){
			private static final long serialVersionUID = 1L;
			
			public void paint(java.awt.Graphics g){

				java.awt.Insets 	insets 		= _frame.getInsets();
				java.awt.Rectangle	clipBounds 	= g.getClipBounds();
				
				frame.paint(new PCGraphics(new Rectangle(clipBounds.x - insets.left, clipBounds.y - insets.top, clipBounds.width, clipBounds.height), _buffer.getGraphics()));
				
				g.drawImage(_buffer, insets.left, insets.top, _frame);
			}
		};
		
		
		_frame.addWindowListener(new java.awt.event.WindowListener(){
			public void windowActivated(WindowEvent e) {
				
			}

			public void windowClosed(WindowEvent e) {
				
			}

			public void windowClosing(WindowEvent e) {
				// TODO: Just to keep from keeping the JRE open, remove when listeners are fully setup
				System.exit(0);
			}

			public void windowDeactivated(WindowEvent e) {
				
			}

			public void windowDeiconified(WindowEvent e) {

				
			}

			public void windowIconified(WindowEvent e) {

				
			}

			public void windowOpened(WindowEvent e) {

				
			}});
		
		_frame.addComponentListener(new java.awt.event.ComponentListener(){

			public void componentHidden(ComponentEvent e) {
				
			}

			public void componentMoved(ComponentEvent e) {
				
			}

			public void componentResized(ComponentEvent e) {
				java.awt.Insets insets = _frame.getInsets();
				
				
				_buffer = _frame.createImage(	_frame.getWidth()  - insets.left - insets.right,
												_frame.getHeight() - insets.top  - insets.bottom);
				
				frame.systemFrameResized(new Dimension(_frame.getWidth(), _frame.getHeight()));
				frame.triggerLayoutManager();
			}

			public void componentShown(ComponentEvent e) {
				frame.systemFrameResized(new Dimension(_frame.getWidth(), _frame.getHeight()));
				frame.triggerLayoutManager();
			}});
	}
	
	public void setVisible(boolean visible)
	{
		_frame.setVisible(visible);
	}
	
	public void setSize(Dimension size)
	{
		_frame.setSize(new java.awt.Dimension(size.getWidth(), size.getHeight()));
	}
	
	public void setPreferredSize(Dimension size)
	{
		_frame.setPreferredSize(new java.awt.Dimension(size.getWidth(), size.getHeight()));
	}
	
	public void setPosition(Point position)
	{
		_frame.setLocation(position.getX(), position.getY());
	}
	
	public void setTitle(String title)
	{
		_frame.setTitle(title);
	}
	
	private java.awt.Frame _frame  = null;
	private java.awt.Image _buffer = null;
}
